package com.eryos.android.cigarettecounter.charts;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.eryos.android.cigarettecounter.R;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public abstract class Chart {
	
	private Context context;

	protected final static String ROOT_URL 		= "http://chart.apis.google.com/chart?";

	public static final int DEFAULT_CHART_WIDTH = 320;
	public static final int DEFAULT_CHART_HEIGHT = 200;

	
	public static final String DATA_SEPARATOR 	= ",";
	public static final String LABEL_SEPARATOR 	= "|";

	protected String type 			= "lc";
	
	protected int width 			= 320;
	protected int height 			= 200;
	protected String bgColor 		= "000000";
	protected String title 			= null;
	protected String titleColor 	= "FFFFFF";
	protected double titleSize 		= 12.5;
	protected double textSize		= 10.5;
	protected String axisColor      = "FFFFFF";
	
	protected String data 			= "";

	protected int maxYValue 		= 22;
	protected int minYValue 		= 0;
	protected int maxXValue 		= 31;
	protected int minXValue 		= 0;

	protected String URL;
	
	public Chart(){
		
	}
	
	public String getURL(){
		generateURL();
		
		return URL;
	}
	
	protected abstract void generateURL();


	public int getWidth() {
		return width;
	}


	public void setWidth(int _width) {		
		this.width = _width;
	}


	public int getHeight() {
		return height;
	}


	public void setHeight(int _height) {
		this.height = _height;
	}


	public String getBgColor() {
		return bgColor;
	}


	public void setBgColor(String bgColor) {
		this.bgColor = bgColor;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getTitleColor() {
		return titleColor;
	}


	public void setTitleColor(String titleColor) {
		this.titleColor = titleColor;
	}


	public String getData() {
		return data;
	}


	public void setData(String _data) {
		
    	if ( _data.length() > DATA_SEPARATOR.length() && _data.endsWith(DATA_SEPARATOR) )
    		_data = _data.substring(0, _data.length()- Chart.DATA_SEPARATOR.length());

		this.data = _data;
	}


	public int getMaxYValue() {
		return maxYValue;
	}


	public void setMaxYValue(int maxYValue) {
		this.maxYValue = maxYValue;
	}


	public int getMinYValue() {
		return minYValue;
	}


	public void setMinYValue(int minYValue) {
		this.minYValue = minYValue;
	}


	public int getMaxXValue() {
		return maxXValue;
	}


	public void setMaxXValue(int maxXValue) {
		this.maxXValue = maxXValue;
	}


	public int getMinXValue() {
		return minXValue;
	}


	public void setMinXValue(int minXValue) {
		this.minXValue = minXValue;
	}

	public double getTitleSize() {
		return titleSize;
	}

	public void setTitleSize(double titleSize) {
		textSize = (titleSize*0.8);
		this.titleSize = titleSize;
	}
	
    
    
	/* ********************************************************************** */
	/*                  STATIC   METHODS                                      */
	/* ********************************************************************** */
    
    /**
     * Last day of the month with data ( date format : 23/01/2011 )
     */
	public static String getMonthLabel( Context ctx, String _date){
    	try {
			String month = _date.substring(3,5);
			Integer i = new Integer(month);

			switch(i){
				case 1:
					return ctx.getResources().getString(R.string.month_january_full);
				case 2:
					return ctx.getResources().getString(R.string.month_february_full);
				case 3:
					return ctx.getResources().getString(R.string.month_march_full);
				case 4:
					return ctx.getResources().getString(R.string.month_april_full);
				case 5:
					return ctx.getResources().getString(R.string.month_may_full);
				case 6:
					return ctx.getResources().getString(R.string.month_june_full);
				case 7:
					return ctx.getResources().getString(R.string.month_july_full);
				case 8:
					return ctx.getResources().getString(R.string.month_august_full);
				case 9:
					return ctx.getResources().getString(R.string.month_september_full);
				case 10:
					return ctx.getResources().getString(R.string.month_october_full);
				case 11:
					return ctx.getResources().getString(R.string.month_november_full);
				case 12:
					return ctx.getResources().getString(R.string.month_december_full);
			}
		} catch (Exception e) {
			System.out.println("getMonthLabel : "+_date);
			e.printStackTrace();
			return "NA";
		}
	
	return "NA";
    }
    
    public static String getMonthFirstLetter(Context ctx, int month){
    	switch(month){
	    	case 1:
	    		return ctx.getResources().getString(R.string.month_january);
	    	case 2:
	    		return ctx.getResources().getString(R.string.month_february);
	    	case 3:
	    		return ctx.getResources().getString(R.string.month_march);
	    	case 4:
	    		return ctx.getResources().getString(R.string.month_april);
	    	case 5:
	    		return ctx.getResources().getString(R.string.month_may);
	    	case 6:
	    		return ctx.getResources().getString(R.string.month_june);
	    	case 7:
	    		return ctx.getResources().getString(R.string.month_july);
	    	case 8:
	    		return ctx.getResources().getString(R.string.month_august);
	    	case 9:
	    		return ctx.getResources().getString(R.string.month_september);
	    	case 10:
	    		return ctx.getResources().getString(R.string.month_october);
	    	case 11:
	    		return ctx.getResources().getString(R.string.month_november);
	    	case 12:
	    		return ctx.getResources().getString(R.string.month_december);
    	}
    	
    	return "NA";
    }
    
    public static String getDayFirstLetter(Context ctx, int day){
    	switch(day){
    	case 0:
    		return ctx.getResources().getString(R.string.week_sunday);
    	case 1:
    		return ctx.getResources().getString(R.string.week_monday);
    	case 2:
    		return ctx.getResources().getString(R.string.week_tuesday);
    	case 3:
    		return ctx.getResources().getString(R.string.week_wednesday);
    	case 4:
    		return ctx.getResources().getString(R.string.week_thursday);
    	case 5:
    		return ctx.getResources().getString(R.string.week_friday);
    	case 6:
    		return ctx.getResources().getString(R.string.week_saturday);
    	}
    	
    	return "NA";
    }
    
    public static Bitmap downloadChartAsBitmap(Context ctx, String url){
    	Log.i("downloadBitmap", url);
        Bitmap bm = null;
        try{
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            Log.d("downloadBitmap","OK");
            return bm;
        } catch(UnknownHostException e){
            Log.w("downloadBitmap", "No available connection", e);
            return getDefaultChartPicture(ctx);
        } catch(IOException e){
            Log.e("downloadBitmap", "Erreur IO", e);
            return getDefaultChartPicture(ctx);
        }
    }	
    
    public static Bitmap getDefaultChartPicture(Context ctx){
    	Drawable errorDrawable = ctx.getResources().getDrawable(R.drawable.ic_menu_discard);
        return ((BitmapDrawable)errorDrawable).getBitmap();
    }
}
